import argparse
import numpy as np

from util.util_log import setup_logging
from util.util_csv import save_csv, save_to_csv
from util.util_image import save_img, save_img_xyz, save_line_chart
from util.util_ris_pattern_2 import point_2_phi_pattern, phase_2_bit, phase_2_pattern, phase_2_pattern_xyz

from beam.RISGeneticAlgorithm import RISGeneticAlgorithm
from beam.RISParticleSwarmOptimizer import RISParticleSwarmOptimizer
from beam.RISSnakeOptimization import RISSnakeOptimization
from beam.RISSparrowSearchAlgorithm import RISSparrowSearchAlgorithm
from beam.RISGreyWolfOptimizer import RISGreyWolfOptimizer
from beam.RISHarrisHawksOptimization import RISHarrisHawksOptimization
from beam.RISIvyOptimization import RISIvyOptimization
from beam.RISAdaptiveGeneticAlgorithm import RISAdaptiveGeneticAlgorithm

from beam.AblationAGAInit import AblationAGAInit
from beam.AblationAGASelect import AblationAGASelection
from beam.AblationAGACross import AblationAGACross
from beam.AblationAGAMutate import AblationAGAMutation

from beam.util_psll import get_psll_by_phaseBit


# 用 phase 计算 phaseBit & patternBit
def get_bit_pattern(phase, bit_num):
    # 相位转换 X bit
    phaseBit, phaseBitDeg = phase_2_bit(phase, bit_num)
    # 计算phase_mix的方向图
    phaseBit = np.deg2rad(phaseBitDeg)
    patternBit = phase_2_pattern(phaseBit)
    return phaseBit, patternBit


# 保存记录
def save(path_pre, theta, phi, name, phase, phaseBit, patternBit, patternBit_xyz, x, y):
    # 保存图片
    save_img(path_pre + "phase_" + name + "_(" + str(theta) + "," + str(phi) + ").jpg", phase)
    save_img(path_pre + "phaseBit_" + name + "_(" + str(theta) + "," + str(phi) + ").jpg", phaseBit)
    save_img(path_pre + "patternBit_" + name + "_(" + str(theta) + "," + str(phi) + ").jpg", patternBit)
    save_img_xyz(path_pre + "patternBit_xyz_" + name + "_(" + str(theta) + "," + str(phi) + ").jpg",
                 np.abs(patternBit_xyz), x, y)
    # 保存相位结果
    save_csv(phase, path_pre + "phase_" + name + "_(" + str(theta) + "," + str(phi) + ").csv")
    save_csv(phaseBit, path_pre + "phaseBit_" + name + "_(" + str(theta) + "," + str(phi) + ").csv")


# 保存记录, 包括遗传算法结果
def save_with_history(path_pre, theta, phi, name, phase, phaseBit, patternBit, patternBit_xyz, x, y,
                      best_fitness_history, best_individual_history):
    # 保存图片 & 相位结果
    save(path_pre, theta, phi, name, phase, phaseBit, patternBit, patternBit_xyz, x, y)
    # 保存遗传算法优化结果
    save_line_chart(path_pre + "best_fitness_history_" + name + "_(" + str(theta) + "," + str(phi) + ").jpg",
                    best_fitness_history, "best_fitness_history", "iteration", "best_fitness", "fitness")
    save_csv([[item] for item in best_fitness_history],
             path_pre + "best_fitness_history_" + name + "_(" + str(theta) + "," + str(phi) + ").csv")
    save_csv(best_individual_history,
             path_pre + "best_individual_history_" + name + "_(" + str(theta) + "," + str(phi) + ").csv")


# 核心方法 -- 单波束 -- GA
def ga_beam1(phase, bit_num, ga):
    # 2.遗传算法(GA)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = ga.run(phase, logger)
    logger.info("GA.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_ga = best_individual
    phaseBit_ga, patternBit_ga = get_bit_pattern(phase_ga, bit_num)
    #
    return phase_ga, phaseBit_ga, patternBit_ga, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- AGA-init
def aga_init_beam1(phase_hho, phase_pso, phase_ga, phase_bp, bit_num, aga_init):
    # 寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history \
        = aga_init.run([phase_hho, phase_pso, phase_ga, phase_bp], logger)
    logger.info("AGA_init.best_fitness=%f" % (best_fitness))
    # 计算优化后的pattern
    phase_aga_init = best_individual
    phaseBit_aga_init, patternBit_aga_init = get_bit_pattern(phase_aga_init, bit_num)
    #
    return phase_aga_init, phaseBit_aga_init, patternBit_aga_init, \
           best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- AGA-select
def aga_select_beam1(phase, bit_num, aga_select):
    # 寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = aga_select.run(phase, logger)
    logger.info("AGA_select.best_fitness=%f" % (best_fitness))
    # 计算优化后的pattern
    phase_aga_select = best_individual
    phaseBit_aga_select, patternBit_aga_select = get_bit_pattern(phase_aga_select, bit_num)
    #
    return phase_aga_select, phaseBit_aga_select, patternBit_aga_select, \
           best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- AGA-cross
def aga_cross_beam1(phase, bit_num, aga_cross):
    # 寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = aga_cross.run(phase, logger)
    logger.info("AGA_cross.best_fitness=%f" % (best_fitness))
    # 计算优化后的pattern
    phase_aga_cross = best_individual
    phaseBit_aga_cross, patternBit_aga_cross = get_bit_pattern(phase_aga_cross, bit_num)
    #
    return phase_aga_cross, phaseBit_aga_cross, patternBit_aga_cross, \
           best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- AGA-mutation
def aga_mutate_beam1(phase, bit_num, aga_mutate):
    # 寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = aga_mutate.run(phase, logger)
    logger.info("AGA_mutate.best_fitness=%f" % (best_fitness))
    # 计算优化后的pattern
    phase_aga_mutate = best_individual
    phaseBit_aga_mutate, patternBit_aga_mutate = get_bit_pattern(phase_aga_mutate, bit_num)
    #
    return phase_aga_mutate, phaseBit_aga_mutate, patternBit_aga_mutate, \
           best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- AGA
def aga_beam1(phase_hho, phase_pso, phase_ga, phase_bp, bit_num, aga):
    # 2.遗传算法(GA)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history \
        = aga.run([phase_hho, phase_pso, phase_ga, phase_bp], logger)
    logger.info("AGA.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_aga = best_individual
    phaseBit_aga, patternBit_aga = get_bit_pattern(phase_aga, bit_num)
    #
    return phase_aga, phaseBit_aga, patternBit_aga, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- PSO
def pso_beam1(phase, bit_num, pso):
    # 2.粒子群算法(PSO)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = pso.run(phase, logger)
    logger.info("PSO.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_pso = best_individual
    phaseBit_pso, patternBit_pso = get_bit_pattern(phase_pso, bit_num)
    #
    return phase_pso, phaseBit_pso, patternBit_pso, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- GWO
def gwo_beam1(phase, bit_num, gwo):
    # 2.灰狼优化算法(GWO)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = gwo.run(phase, logger)
    logger.info("GWO.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_gwo = best_individual
    phaseBit_gwo, patternBit_gwo = get_bit_pattern(phase_gwo, bit_num)
    #
    return phase_gwo, phaseBit_gwo, patternBit_gwo, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- HHO
def hho_beam1(phase, bit_num, hho):
    # 2.哈里斯鹰算法(HHO)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = hho.run(phase, logger)
    logger.info("HHO.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_hho = best_individual
    phaseBit_hho, patternBit_hho = get_bit_pattern(phase_hho, bit_num)
    #
    return phase_hho, phaseBit_hho, patternBit_hho, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- SO
def so_beam1(phase, bit_num, so):
    # 2.蛇优化算法(SO)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = so.run(phase, logger)
    logger.info("SO.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_so = best_individual
    phaseBit_so, patternBit_so = get_bit_pattern(phase_so, bit_num)
    #
    return phase_so, phaseBit_so, patternBit_so, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- SSA
def ssa_beam1(phase, bit_num, ssa):
    # 2.麻雀算法(SSA)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = ssa.run(phase, logger)
    logger.info("SSA.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_ssa = best_individual
    phaseBit_ssa, patternBit_ssa = get_bit_pattern(phase_ssa, bit_num)
    #
    return phase_ssa, phaseBit_ssa, patternBit_ssa, best_fitness, best_fitness_history, best_individual_history


# 核心方法 -- 单波束 -- IVY
def ivy_beam1(phase, bit_num, ivy):
    # 2.常春藤算法(IVY)寻找最优的phaseBit
    best_individual, best_fitness, best_fitness_history, best_individual_history = ivy.run(phase, logger)
    logger.info("IVY.best_fitness=%f" % (best_fitness))
    # 3.计算优化后的pattern
    phase_ivy = best_individual
    phaseBit_ivy, patternBit_ivy = get_bit_pattern(phase_ivy, bit_num)
    #
    return phase_ivy, phaseBit_ivy, patternBit_ivy, best_fitness, best_fitness_history, best_individual_history


# ============================================= 主函数 ====================================
# 单波束 -- 消融实验 -- AGA
def main_beam1_ablation_aga(theta_start, theta_step, theta_end, phi_start, phi_step, phi_end, path_pre, bit_num):
    logger.info(f"main_beam1_ablation_aga: bit_num={bit_num}, path_pre={path_pre}")
    logger.info(f"main_beam1_ablation_aga: theta_start={theta_start}, theta_step={theta_step}, theta_end={theta_end}")
    logger.info(f"main_beam1_ablation_aga: phi_start={phi_start}, phi_step={phi_step}, phi_end={phi_end}")
    max_iter = 30
    max_iter_aga = 120
    beam_num = 1
    ga = RISGeneticAlgorithm(bit_num=bit_num, beam_num=beam_num,
                             population_size=50, num_generations=max_iter, num_parents=10, mutation_rate=0.1)
    pso = RISParticleSwarmOptimizer(bit_num=bit_num, beam_num=beam_num,
                                    num_particles=50, max_iter=max_iter, w=0.7, c1=2, c2=2)
    gwo = RISGreyWolfOptimizer(bit_num=bit_num, beam_num=beam_num, num_wolves=50, max_iter=max_iter, a_decay=2)
    hho = RISHarrisHawksOptimization(bit_num=bit_num, beam_num=beam_num,
                                     num_hawks=50, max_iter=max_iter, lb=-180, ub=180, phase_mix_init=None)
    # so = RISSnakeOptimization(bit_num=bit_num, beam_num=beam_num, population_size=50, num_generations=max_iter)
    # ssa = RISSparrowSearchAlgorithm(bit_num=bit_num, beam_num=beam_num,
    #                                 population_size=50, num_generations=max_iter, alpha=2, beta=1.5, gamma=1.0)
    # ivy = RISIvyOptimization(bit_num=bit_num, beam_num=beam_num, N=50, max_iterations=max_iter, rows=64, cols=64)
    #
    aga_init = AblationAGAInit(bit_num=bit_num, beam_num=beam_num, population_size=50,
                               num_generations=max_iter, num_parents=10, mutation_rate=0.1)
    aga_select = AblationAGASelection(bit_num=bit_num, beam_num=beam_num, population_size=50,
                                      num_generations=max_iter_aga, num_parents=10, mutation_rate=0.1)
    aga_cross = AblationAGACross(bit_num=bit_num, beam_num=beam_num, population_size=50,
                                 num_generations=max_iter_aga, num_parents=10, mutation_rate=0.1)
    aga_mutate = AblationAGAMutation(bit_num=bit_num, beam_num=beam_num, population_size=50,
                                     num_generations=max_iter_aga, num_parents=10, mutation_rate=0.1)
    #
    aga = RISAdaptiveGeneticAlgorithm(bit_num=bit_num, beam_num=beam_num,
                                      population_size=50, num_generations=max_iter_aga, num_parents=10, mutation_rate=0.1)
    for theta in range(theta_start, theta_end, theta_step):
        for phi in range(phi_start, phi_end, phi_step):
            logger.info(f"theta:{theta}, phi:{phi}")
            # 1.背投影法(BP)计算码阵
            phase, phaseBit, patternBit = point_2_phi_pattern(theta, phi, bit_num)
            # 2.1 遗传算法(GA)优化
            phase_ga, phaseBit_ga, patternBit_ga, \
            best_fitness_ga, best_fitness_history_ga, best_individual_history_ga = ga_beam1(phase, bit_num, ga)
            # 2.2 粒子群算法(PSO)优化
            phase_pso, phaseBit_pso, patternBit_pso, \
            best_fitness_pso, best_fitness_history_pso, best_individual_history_pso = pso_beam1(phase, bit_num, pso)
            # # 2.3 灰狼优化算法(GWO)优化
            # phase_gwo, phaseBit_gwo, patternBit_gwo, \
            # best_fitness_gwo, best_fitness_history_gwo, best_individual_history_gwo = gwo_beam1(phase, bit_num, gwo)
            # 2.4 哈里斯鹰算法(HHO)优化
            phase_hho, phaseBit_hho, patternBit_hho, \
            best_fitness_hho, best_fitness_history_hho, best_individual_history_hho = hho_beam1(phase, bit_num, hho)
            # # 2.5 蛇优化算法(SO)优化
            # phase_so, phaseBit_so, patternBit_so, \
            # best_fitness_so, best_fitness_history_so, best_individual_history_so = so_beam1(phase, bit_num, so)
            # # 2.6 麻雀算法(SSA)优化
            # phase_ssa, phaseBit_ssa, patternBit_ssa, \
            # best_fitness_ssa, best_fitness_history_ssa, best_individual_history_ssa = ssa_beam1(phase, bit_num, ssa)
            # # 2.7 常春藤算法(IVY)优化
            # phase_ivy, phaseBit_ivy, patternBit_ivy, \
            # best_fitness_ivy, best_fitness_history_ivy, best_individual_history_ivy = ivy_beam1(phase, bit_num, ivy)
            # 2.8 改进遗传算法(AGA)优化
            phase_aga, phaseBit_aga, patternBit_aga, \
            best_fitness_aga, best_fitness_history_aga, best_individual_history_aga \
                = aga_beam1(phase_hho, phase_pso, phase_ga, phase, bit_num, aga)
            #
            phase_aga_init, phaseBit_aga_init, patternBit_aga_init, \
            best_fitness_aga_init, best_fitness_history_aga_init, best_individual_history_aga_init \
                = aga_init_beam1(phase_hho, phase_pso, phase_ga, phase, bit_num, aga_init)
            phase_aga_select, phaseBit_aga_select, patternBit_aga_select, \
            best_fitness_aga_select, best_fitness_history_aga_select, best_individual_history_aga_select \
                = aga_select_beam1(phase, bit_num, aga_select)
            phase_aga_cross, phaseBit_aga_cross, patternBit_aga_cross, \
            best_fitness_aga_cross, best_fitness_history_aga_cross, best_individual_history_aga_cross \
                = aga_cross_beam1(phase, bit_num, aga_cross)
            phase_aga_mutate, phaseBit_aga_mutate, patternBit_aga_mutate, \
            best_fitness_aga_mutate, best_fitness_history_aga_mutate, best_individual_history_aga_mutate \
                = aga_mutate_beam1(phase, bit_num, aga_mutate)
            #
            # 计算pattern
            patternBit_xyz, x, y, z = phase_2_pattern_xyz(phaseBit)
            patternBit_xyz_ga, x_ga, y_ga, z_ga = phase_2_pattern_xyz(phaseBit_ga)
            patternBit_xyz_pso, x_pso, y_pso, z_pso = phase_2_pattern_xyz(phaseBit_pso)
            # patternBit_xyz_gwo, x_gwo, y_gwo, z_gwo = phase_2_pattern_xyz(phaseBit_gwo)
            patternBit_xyz_hho, x_hho, y_hho, z_hho = phase_2_pattern_xyz(phaseBit_hho)
            # patternBit_xyz_so, x_so, y_so, z_so = phase_2_pattern_xyz(phaseBit_so)
            # patternBit_xyz_ssa, x_ssa, y_ssa, z_ssa = phase_2_pattern_xyz(phaseBit_ssa)
            # patternBit_xyz_ivy, x_ivy, y_ivy, z_ivy = phase_2_pattern_xyz(phaseBit_ivy)
            patternBit_xyz_aga, x_aga, y_aga, z_aga = phase_2_pattern_xyz(phaseBit_aga)
            patternBit_xyz_aga_init, x_aga_init, y_aga_init, z_aga_init = phase_2_pattern_xyz(phaseBit_aga_init)
            patternBit_xyz_aga_select, x_aga_select, y_aga_select, z_aga_select = phase_2_pattern_xyz(phaseBit_aga_select)
            patternBit_xyz_aga_cross, x_aga_cross, y_aga_cross, z_aga_cross = phase_2_pattern_xyz(phaseBit_aga_cross)
            patternBit_xyz_aga_mutate, x_aga_mutate, y_aga_mutate, z_aga_mutate = phase_2_pattern_xyz(phaseBit_aga_mutate)
            # 保存结果
            logger.info("save beam 1 result...")
            save(path_pre, theta, phi, "Ori", phase, phaseBit, patternBit, patternBit_xyz, x, y)
            save_with_history(path_pre, theta, phi, "GA", phase_ga, phaseBit_ga,
                              patternBit_ga, patternBit_xyz_ga, x_ga, y_ga,
                              best_fitness_history_ga, best_individual_history_ga)
            save_with_history(path_pre, theta, phi, "PSO", phase_pso, phaseBit_pso,
                              patternBit_pso, patternBit_xyz_pso, x_pso, y_pso,
                              best_fitness_history_pso, best_individual_history_pso)
            # save_with_history(path_pre, theta, phi, "GWO", phase_gwo, phaseBit_gwo,
            #                   patternBit_gwo, patternBit_xyz_gwo, x_gwo, y_gwo,
            #                   best_fitness_history_gwo, best_individual_history_gwo)
            save_with_history(path_pre, theta, phi, "HHO", phase_hho, phaseBit_hho,
                              patternBit_hho, patternBit_xyz_hho, x_hho, y_hho,
                              best_fitness_history_hho, best_individual_history_hho)
            # save_with_history(path_pre, theta, phi, "SO", phase_so, phaseBit_so,
            #                   patternBit_so, patternBit_xyz_so, x_so, y_so,
            #                   best_fitness_history_so, best_individual_history_so)
            # save_with_history(path_pre, theta, phi, "SSA", phase_ssa, phaseBit_ssa,
            #                   patternBit_ssa, patternBit_xyz_ssa, x_ssa, y_ssa,
            #                   best_fitness_history_ssa, best_individual_history_ssa)
            # save_with_history(path_pre, theta, phi, "IVY", phase_ivy, phaseBit_ivy,
            #                   patternBit_ivy, patternBit_xyz_ivy, x_ivy, y_ivy,
            #                   best_fitness_history_ivy, best_individual_history_ivy)
            save_with_history(path_pre, theta, phi, "AGA", phase_aga, phaseBit_aga,
                              patternBit_aga, patternBit_xyz_aga, x_aga, y_aga,
                              best_fitness_history_aga, best_individual_history_aga)
            save_with_history(path_pre, theta, phi, "AGA_init", phase_aga_init, phaseBit_aga_init,
                              patternBit_aga_init, patternBit_xyz_aga_init, x_aga_init, y_aga_init,
                              best_fitness_history_aga_init, best_individual_history_aga_init)
            save_with_history(path_pre, theta, phi, "AGA_select", phase_aga_select, phaseBit_aga_select,
                              patternBit_aga_select, patternBit_xyz_aga_select, x_aga_select, y_aga_select,
                              best_fitness_history_aga_select, best_individual_history_aga_select)
            save_with_history(path_pre, theta, phi, "AGA_cross", phase_aga_cross, phaseBit_aga_cross,
                              patternBit_aga_cross, patternBit_xyz_aga_cross, x_aga_cross, y_aga_cross,
                              best_fitness_history_aga_cross, best_individual_history_aga_cross)
            save_with_history(path_pre, theta, phi, "AGA_mutate", phase_aga_mutate, phaseBit_aga_mutate,
                              patternBit_aga_mutate, patternBit_xyz_aga_mutate, x_aga_mutate, y_aga_mutate,
                              best_fitness_history_aga_mutate, best_individual_history_aga_mutate)
            # 保存指标
            psll = get_psll_by_phaseBit(phaseBit, bit_num, 1)
            psll_ga = get_psll_by_phaseBit(phaseBit_ga, bit_num, 1)
            psll_pso = get_psll_by_phaseBit(phaseBit_pso, bit_num, 1)
            # psll_gwo = get_psll_by_phaseBit(phaseBit_gwo, bit_num, 1)
            psll_hho = get_psll_by_phaseBit(phaseBit_hho, bit_num, 1)
            # psll_so = get_psll_by_phaseBit(phaseBit_so, bit_num, 1)
            # psll_ssa = get_psll_by_phaseBit(phaseBit_ssa, bit_num, 1)
            # psll_ivy = get_psll_by_phaseBit(phaseBit_ivy, bit_num, 1)
            psll_aga = get_psll_by_phaseBit(phaseBit_aga, bit_num, 1)
            psll_aga_init = get_psll_by_phaseBit(phaseBit_aga_init, bit_num, 1)
            psll_aga_select = get_psll_by_phaseBit(phaseBit_aga_select, bit_num, 1)
            psll_aga_cross = get_psll_by_phaseBit(phaseBit_aga_cross, bit_num, 1)
            psll_aga_mutate = get_psll_by_phaseBit(phaseBit_aga_mutate, bit_num, 1)
            result_csv = {
                "(theta,phi)": f"({theta},{phi})",
                "theta": theta,
                "phi": phi,
                "psll": psll,
                "psll_ga": psll_ga,
                "psll_pso": psll_pso,
                # "psll_gwo": psll_gwo,
                "psll_hho": psll_hho,
                # "psll_so": psll_so,
                # "psll_ssa": psll_ssa,
                # "psll_ivy": psll_ivy,
                "psll_aga": psll_aga,
                "best_fitness_ga": best_fitness_ga,
                "best_fitness_pso": best_fitness_pso,
                # "best_fitness_gwo": best_fitness_gwo,
                "best_fitness_hho": best_fitness_hho,
                # "best_fitness_so": best_fitness_so,
                # "best_fitness_ssa": best_fitness_ssa,
                # "best_fitness_ivy": best_fitness_ivy,
                "best_fitness_aga": best_fitness_aga,
                "best_fitness_aga_init": best_fitness_aga_init,
                "best_fitness_aga_select": best_fitness_aga_select,
                "best_fitness_aga_cross": best_fitness_aga_cross,
                "best_fitness_aga_mutate": best_fitness_aga_mutate,
            }
            save_to_csv([result_csv], path_pre + "result.csv")



# ======================================================= main 主方法 ===============================================
def main(theta_start, theta_step, theta_end, phi_start, phi_step, phi_end, base_path):
    main_beam1_ablation_aga(theta_start, theta_step, theta_end, phi_start, phi_step, phi_end, base_path, 1)


if __name__ == '__main__':
    # 创建参数解析器
    parser = argparse.ArgumentParser(description="Process some parameters.")
    parser.add_argument("--base_path", type=str,
                        default="../files/sim/test/",
                        help="Base directory path (default: ../files/sim/test/")
    parser.add_argument("--theta_start", type=int, default=1, help="theta_start (default: 1)")
    parser.add_argument("--theta_step", type=int, default=1, help="theta_step (default: 1)")
    parser.add_argument("--theta_end", type=int, default=60, help="theta_end (default: 60)")
    parser.add_argument("--phi_start", type=int, default=0, help="phi_start (default: 0)")
    parser.add_argument("--phi_step", type=int, default=1, help="phi_step (default: 1)")
    parser.add_argument("--phi_end", type=int, default=360, help="phi_end (default: 360)")

    args = parser.parse_args()

    base_path = args.base_path
    theta_start, theta_step, theta_end = args.theta_start, args.theta_step, args.theta_end
    phi_start, phi_step, phi_end = args.phi_start, args.phi_step, args.phi_end

    # 初始化日志
    logger = setup_logging(base_path + "/log.txt")
    # 示例日志记录
    logger.info(f"Starting execution with base_path: {base_path}")
    logger.info(f"theta_start={theta_start}, theta_step={theta_step}, theta_end={theta_end}")
    logger.info(f"phi_start={phi_start}, phi_step={phi_step}, phi_end={phi_end}")

    main(theta_start, theta_step, theta_end, phi_start, phi_step, phi_end, base_path)